package com.earl.eatbao.service.impl;

import java.util.Date;
import java.util.List;

import com.earl.eatbao.commons.dao.BaseRepsitoty;
import com.earl.eatbao.dao.eatbao.ShopDao;
import com.earl.eatbao.domain.eatbao.Shop;
import com.earl.eatbao.util.UpLoadFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.earl.eatbao.commons.service.BaseServiceImpl;
import com.earl.eatbao.dao.eatbao.GoodsDao;
import com.earl.eatbao.domain.eatbao.Goods;
import com.earl.eatbao.service.GoodsService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品服务实现类
 * @author Administrator
 * @version 1.0
 * @updated 23-十一月-2016 23:07:12
 */
@Service("goodsService")
public class GoodsServiceImpl extends BaseServiceImpl<Goods, Long> implements GoodsService {

	// desc:日记工具logger
	private static final Logger logger = LoggerFactory.getLogger(GoodsServiceImpl.class);

	@Autowired
	private GoodsDao goodsDao;

	@Autowired
    private ShopDao shopDao;

	@Override
	protected BaseRepsitoty<Goods, Long> getDao() {
		// TODO Auto-generated method stub
		return goodsDao;
	}

	@Override
	public Boolean passGoodsCheck(long goodId){
		logger.info("<====== 进入passGoodsCheck:方法,{[goodId] : " + goodId + " } ====");

		Boolean result = changeGoodsCheck(goodId, true);

		logger.info("==== 退出passGoodsCheck方法 返回值类型:{Boolean: " + result + " } =======>");
		return result;
	}

	@Override
	public Boolean addGoods(Goods goods, MultipartFile file) {
	    Shop  shop = shopDao.findOne(goods.getShopId());
	    if (null != shop && (shop.isDelete() || shop.getStatus() == 3)) {
	        logger.info("==== 该商店没有上架商品的能力 ====");
	        return false;
        }

		if (null != goods && null != shop) {
			logger.info("<====== 进入addGoods:方法,{[goods] :" + goods.toString() + "} ====");
			goods.setDelete(false);
			goods.setStatus(1);
			goods.setPass(false);
			goods.setWatch(false);
			goods.setCreatedDate(new Date());
			goods.setLastModifiedDate(new Date());
			String image_path = UpLoadFile.saveFile(file, UpLoadFile.getUUID());
			if (null != image_path) {
			    logger.info("===== image_ptah:" + image_path + "=====");
                goods.setGoodImg(image_path);
            }
			goodsDao.save(goods);
			logger.info("==== 退出addGoods方法 返回值类型:{ java.lang.Boolean: true } =======>");
			return true;
		} else {
			logger.info("<====== 进入addGoods:方法,或者创建商店为空{[shop] :" + "为空" + "} ====");
			logger.info("==== 退出addGoods方法 返回值类型:{ Boolean:false } =======>");
			return false;
		}
	}

	@Override
	public Boolean failGoodsCheck(Long goodId) {
		logger.info("<====== 进入failGoodsCheck:方法,{[goodId] : " + goodId + " } ====");
		Boolean result = changeGoodsCheck(goodId, false);
		logger.info("==== 退出failGoodsCheck方法 返回值类型:{ Boolean: " + result + " } =======>");
		return result;
	}

	@Override
	public Boolean shopPutAwayGoods(Long goodId) {
		logger.info("<====== 进入shopPutAwayGoods:方法,{[goodId] :" + goodId + " } ====");
		Boolean result = shopChagenGoodsUse(goodId, 1, true);
		logger.info("==== 退出shopPutAwayGoods方法 返回值类型:{Boolean: " + result + " } =======>");
		return result;
	}

	@Override
	public Boolean shopSoldOutGoods(Long goodId) {
		logger.info("<====== 进入shopSoldOutGoods:方法,{[goodId] :" + goodId + " } ====");
		Boolean result = shopChagenGoodsUse(goodId, 2, false);
		logger.info("==== 退出shopSoldOutGoods方法 返回值类型:{Boolean: " + result + " } =======>");
		return result;
	}

	@Override
	public Boolean adminPutAwayGoods(Long goodId) {
		logger.info("<====== 进入adminPutAwayGoods:方法,{[goodId] :" + goodId + " } ====");
		Boolean result = adminChangeGoodsUse(goodId, 2);
		logger.info("==== 退出adminPutAwayGoods方法 返回值类型:{Boolean: " + result + " } =======>");
		return result;
	}

	@Override
	public Boolean adminSoldOutGoods(Long goodId) {
		logger.info("<====== 进入adminSoldOutGoods:方法,{[goodId] :" + goodId + " } ====");
		Boolean result = adminChangeGoodsUse(goodId, 3);
		logger.info("==== 退出adminSoldOutGoods方法 返回值类型:{Boolean: " + result + " } =======>");
		return result;
	}

	@Override
	public List<Goods> findByStatus(int status) {
		return goodsDao.findByStatus(status);
	}

	@Override
	public List<Goods> findByWatch(Boolean watch) {
		return goodsDao.findByWatch(watch);
	}

	/**
	 * 审核是否通过更改商品是否上架状态
	 * @param goodId
	 * @param status
	 */
	private Boolean changeGoodsCheck(Long goodId, Boolean status) {
		Goods goods = findOne(goodId);
		//只能更新没用通过的和被查验的用户
		if (null != goods && goods.isPass() == false && goods.isWatch() == false) {
			goods.setPass(status);
			goods.setWatch(true);
			save(goods);
			return true;
		} else {
			logger.info("==== 该商家不存在,非法更改商家状态 ====");
			return false;
		}
	}

	/**
	 * 商家改变订单状态
	 * @param goodId
	 * @param status 状态码
	 * @param isUse 是否上架状态
	 * @return
	 */
	private Boolean shopChagenGoodsUse(Long goodId, int status, Boolean isUse) {
		Goods goods = goodsDao.findOne(goodId);
		if (null != goods && goods.getStatus() != 3 && goods.isPass()) {
			goods.setStatus(status);
			goodsDao.save(goods);
			return true;
		} else {
			logger.info(("=== 该商品不存在,或者操作不合法 ==="));
			return false;
		}
	}

	/**
	 * 管理员操作商家商品上下架
	 * @param goodId
	 * @param status
	 * @return
	 */
	private Boolean adminChangeGoodsUse(Long goodId, int status) {
		Goods goods = goodsDao.findOne(goodId);
		if (null != goods && goods.isPass()) {
			goods.setStatus(status);
			goodsDao.save(goods);
			return true;
		} else {
			logger.info(("=== 该商品不存在,或者操作不合法 ==="));
			return false;
		}
	}

	@Override
	public List<Goods> findShopGoods(Long shopId) {
		// TODO Auto-generated method stub
		List<Goods> goodsList = goodsDao.findByShopIdAndStatusAndPassTrueAndIsDeleteFalse(shopId, 1);
		return goodsList;
	}

	@Override
	public Boolean upLoadGoodImage(MultipartFile file, Long goodsId) {
		logger.info("<====== 进入upLoadGoodImage:方法,{[file, goodsId] :" + goodsId +" } ====");

		Boolean result = false;

		Goods goods = goodsDao.findOne(goodsId);
		if (null != goods) {
			String imagePath = UpLoadFile.saveFile(file, UpLoadFile.getUUID());
			if (null != imagePath) {
				goods.setGoodImg(imagePath);
				goodsDao.save(goods);
				result = true;
			} else {
				logger.info("==== 保存图片失败 ====");
			}
		} else {
			logger.info("==== 没有该商品 ====");
		}
		logger.info("==== 退出upLoadGoodImage方法 返回值类型:{Boolean:"+ result + " } =======>");
		return result;
	}

	@Override
	public List<Goods> findShopUnCheckGoods(Long shopId) {
		return goodsDao.findByShopIdAndWatchFalseAndIsDeleteFalse(shopId);
	}

	@Override
	public List<Goods> findOnShevlesGoods() {
		return goodsDao.findByStatus(1);
	}
}
